package com.samphanie.security.validation.sms;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.samphanie.security.domain.Captcha;
import com.samphanie.security.validation.properties.ValidateProperties;
import com.samphanie.security.validation.service.ValidateCodeGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Date;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Slf4j
@RequiredArgsConstructor
@Component("smsValidateCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator {

    private final ValidateProperties validateProperties;

    @Override
    public Captcha generate(ServletWebRequest request) {
        String code = RandomUtil.randomNumbers(validateProperties.getSms().getLength());
        return Captcha.builder()
                .uuid(IdUtil.simpleUUID())
                .code(code)
                .expireTime(Date.from(new Date().toInstant().plusSeconds(validateProperties.getKaptcha().getExpireIn())))
                .build();
    }
}
